<%@ include file="/taglibs.jsp"%>

<head>
    <title><fmt:message key="fileForm.title"/></title>
    <script type="text/javascript" src="${ctx}/scripts/file_module.js"></script>
    <script type="text/javascript">
            function ValidateText(i)
            {
                if(i.value.length>0)
                {
                    i.value = i.value.replace(/[^\d]+/g, '');
                }
            } 
        </script>
</head>
<s:form name="fileForm" action="saveFile" method="post" validate="true">

<s:hidden name="file.id" value="%{file.id}"/>

<tr>
	<td colspan="2">
		<p><s:text name="file.group.category"></s:text></p>
	</td>
</tr>

<s:select key="file.field" required="true" name="fieldId" cssStyle="max-width: 500px !important;" list="fields" listKey="id" listValue="name" headerKey="%{getText('select.default.key')}" headerValue="%{getText('select.default.value')}"></s:select>
<s:select key="file.category" required="true" name="cateId" cssStyle="max-width: 500px !important;" list="catList" listKey="value" listValue="name" headerKey="%{getText('select.default.key')}" headerValue="%{getText('select.default.value')}"></s:select>
<s:select key="file.subCategory" name="subCateId" cssStyle="max-width: 500px !important;" list="subCateList" listKey="value" listValue="name" headerKey="%{getText('select.default.key')}" headerValue="%{getText('select.default.value')}"></s:select>
<s:select key="documentType.name" required="true" name="file.categoryType.id" id="categoryTypeId" list="categoryTypeList" listKey="id" listValue="name" headerKey="%{getText('select.default.key')}" headerValue="%{getText('select.default.value')}"></s:select>
<tr>
	<td>
        <s:property value="getText('file.inputDate')"/>:
    </td>
	<td>
		<s:textfield id="fromDate" readonly="true" name="file.inputDate" size="11" theme="simple"/>
		
        <c:if test="${totalOverStep > 1}">
			<button id="fromDateCal" disabled="disabled" type="button" class="button"> ... </button>
			 <s:property value="getText('file.not.allowedit.inputdate')"/>
		</c:if>
		<c:if test="${totalOverStep <= 1}">
			<button id="fromDateCal" type="button" class="button"> ... </button> 
		</c:if>
		&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
		<s:property value="getText('file.outputDate')"/>:<span class="required">*</span>
		<s:textfield id="toDate" readonly="true" name="file.outputDate" size="11" theme="simple"/>
        <button id="toDateCal" type="button" class="button"> ... </button> 
	</td>
</tr>


<!-- <tr>
	
</tr> -->
<tr>
	<td colspan="2">
		<p><s:text name="file.group.sender.info"></s:text></p>
	</td>
</tr>
<tr>
	<td width="140px">
        <s:property value="getText('file.sender')"/><span class="required">*</span>
    </td>
    <td>
    	<s:textfield  key="file.sender" theme="simple" id="sender" size="30"/>
    </td>
</tr>
<s:textfield  key="file.senderPhone" onkeyup="ValidateText(this);" required="true" id="senderPhone" size="20"/>

<tr>
		<td colspan="2">
			<p><s:text name="file.group.contact.info"></s:text></p>
		</td>
</tr>

	<tr>
		<td><s:property value="getText('file.submitter')" /><span
			class="required">*</span></td>
		<td><s:textfield key="file.submitter" theme="simple" size="70"
				id="submitter" ></s:textfield></td>
	</tr>
	
	<tr>
		<td>
	        <s:property value="getText('file.contactName')"/><span class="required">*</span>
	    </td>
	    <td>
	    	<s:textfield  key="file.contactName" theme="simple" id="contactName" size="30"/> 
	    	<s:text name="basic.general.same.as.above"></s:text>
	    	<s:checkbox name="none" id="none" onclick='$("#contactName").val($("#sender").val())' theme="simple"/>
	    </td>
	</tr>
	<tr>
		<td>
	        <s:property value="getText('file.contactPhone')"/><span class="required">*</span>
	    </td>
	    <td>
	    	<s:textfield  key="file.contactPhone" theme="simple" onkeyup="ValidateText(this);" id="contactPhone" size="20"/> 
	    	<s:text name="basic.general.same.as.above"></s:text>
	    	<s:checkbox name="none" id="none" onclick='$("#contactPhone").val($("#senderPhone").val())' theme="simple"/>
	    </td>
	</tr>
	
	<s:textfield  key="file.contactFax" onkeyup="ValidateText(this);" size="20"/>
	<s:textfield  key="file.address" size="70"/>


	<tr>
		<td colspan="2">
			<p>
				<s:text name="file.group.building.info"></s:text>
			</p></td>
	</tr>

	<s:textfield key="file.construct" size="70" />
	<s:textfield key="file.location" size="70" />

	<tr>
		<td colspan="2">
			<p>
				<s:text name="file.organization"></s:text>
			</p></td>
	</tr>
	<s:textfield  key="file.organization" size="70"/>
	<s:textfield key="file.orgPhone" onkeyup="ValidateText(this);" size="20" />
	<s:textfield key="file.orgFax" onkeyup="ValidateText(this);" size="20" />

	<tr>
		<td colspan="2">
			<p>
				<s:text name="file.oversea"></s:text>
			</p></td>
	</tr>
	<s:textfield key="file.oversea" size="70" />
	<s:textfield key="file.overseaPhone" onkeyup="ValidateText(this);" size="20" />
	<s:textfield key="file.overseaFax" onkeyup="ValidateText(this);" size="20" />

	<tr>
		<td colspan="2">
			<p>
				<s:text name="file.consultant.title"></s:text>
			</p></td>
	</tr>
	<s:textfield key="file.consultantName" size="70" />
	<s:textfield key="file.consultantPhone" size="20" />
	<s:textfield key="file.consultantAdd" size="60" />


	<tr>
		<td colspan="2">
			<p>
				<s:text name="file.group.attachfile.info"></s:text>
			</p></td>
	</tr>

	<tr>
	<td colspan="2">
		<table>
		<s:text name="file.attachFileDetail"></s:text>
		<%-- <s:set var="attachFileDetails" value="file.attachFileDetails" scope="request"/> --%>
		
		<c:if test="${not empty file.id}">
			<s:set var="attachFileDetails" value="listAttachFile" scope="request"/>
			<s:set var="attachFiles" value="attachFiles" scope="request"/>
			<s:set var="documentTypes" value="documentTypes" scope="request"/>
		</c:if>
		
		<display:table name="attachFileDetails" class="table" htmlId="attachFiles" id="attachFileDetail" partialList="false">
		    <display:column property="attachFile.attachFileName" titleKey="attachFile.attachFileName" escapeXml="true"/>
		    <display:column property="attachFile.numberOfCopy" titleKey="attachFile.numberOfCopy" escapeXml="true"/>
		    <display:column titleKey="attachFile.detail.numberOfCopy">
		    	<input size="4" name="numberOfCopy" value="${attachFileDetail.numberOfcopy}">
		    	<input type="hidden" name="attachFileId" value="${attachFileDetail.attachFile.id}">
		    	<input type="hidden" name="attachFileDetailId" value="${attachFileDetail.id}">
		    	
		    </display:column>
		    <display:column titleKey="documentType.name">
		    	<select name="documentTypeId">
			    	<c:forEach items="${documentTypes}" var="item">
<%-- 			    		 <option value="${item.id}" selected='${item.id == attachFileDetail.documentType.id ? "selected" : ""}'>${item.name}${attachFileDetail.documentType.id}</option> --%>
			    		<c:if test="${item.id == attachFileDetail.documentType.id}">
			    			<option value="${item.id}" selected="selected">${item.name}</option>
			    		</c:if>
			    		<c:if test="${item.id != attachFileDetail.documentType.id}">
			    			<option value="${item.id}" >${item.name}</option>
			    		</c:if>
			    	</c:forEach>
		    	</select>
		    </display:column>
		    <display:column title="<input type='checkbox' class='checkAll'>" >
		    	<!-- <input type="checkbox" class="cb-element" checked="checked"> -->
		    	<c:if test="${not empty attachFileDetail.documentType}">
		    		<input name="checkbox" type="checkbox" class="cb-element" checked="checked">
					<s:hidden name="checked" value="true" theme="simple"></s:hidden>
		    	</c:if>
		    	<c:if test="${empty attachFileDetail.documentType}">
		    		<input name="checkbox" type="checkbox" class="cb-element">
					<s:hidden name="checked" value="false" theme="simple"></s:hidden>
		    	</c:if>
		    </display:column>
		</display:table>
		</table>
	</td>
</tr>

<s:textarea  key="file.otherAttachFiles" cols="60" rows="4"></s:textarea>



<s:url action="exportReceipt" var="exportReceiptURL">
	<s:param name="id" value="%{file.id}"></s:param>
</s:url>
<table>
<tr>
    <td></td>
    <td>
        <s:a href="javascript:void(0)" id="save" >
			<img src="${ctx}/images/btn_save_link.png">
		</s:a>
    <c:if test="${not empty param.id}">
    	
    	<s:url action="delete_file" var="delURL">
	    		<s:param name="id" value="%{file.id}"></s:param>
	    	</s:url>
	    	<s:a href="%{delURL}" id="delete" onclick="return confirm('Ban co chac muon Xoa Ho so nay khong?');">
				<img src="${ctx}/images/btn_delete_link.png">
			</s:a>
    	
        <%-- <s:a href="%{exportReceiptURL}" >
    		<img src="${ctx}/images/export_word.png">
    	</s:a> --%>
    </c:if>
        <s:a action="default" namespace="/" id="cancel">
			<img src="${ctx}/images/btn_cancel_link.png">
		</s:a>
    </td>
</tr>
</table>
</s:form>

<script type="text/javascript">
$('input[name="numberOfCopy"]').live('keypress', $.NumberUtils.acceptNumberOnly);
$('input[name="numberOfCopy"]').live('blur', function(){
	if ($.trim($(this).val()).length != 0){
		$(this).parent().parent().find('input:checkbox').attr('checked', true);
	}else {
		$(this).parent().parent().find('input:checkbox').attr('checked', false);
	}
	$(this).parent().parent().find('input:checkbox').change();
});



$('input[name="checkbox"]').click( function()
		{
		if (jQuery(this).is(':checked'))
			$(this).parent().parent().find('input[name="checked"]').attr('value', true);
		else $(this).parent().parent().find('input[name="checked"]').attr('value', false);
	
		});


$(document).ready(function() {
	$('select[name="cateId"], select[id="categoryTypeId"]').change(function(){
		if ($('select[name="cateId"]').val() != "0" && $('#categoryTypeId').val() != "0") {
			$.blockUI({ message: ajaxAnimation });
			$.ajax({
			  url: '${ctx}/get-attach-file.html',
			  data : {'cateId' : $('select[name="cateId"]').val(), 'cateTypeId' : $('#categoryTypeId').val()},
			  dataType : 'text/html',
			  success: function(data) {
					$('#attachFiles tbody').html(data);
			  }
			});
		} else {
			$('#attachFiles tbody').empty().html('<tr class="empty"><td colspan="5"><s:text name="basic.msg.empty_list"></s:text> </td></tr>');
		}
	});
	
	$('select[name="cateId"]').change(function(){
		if ($(this).val() != "0") {
			$.blockUI({ message: ajaxAnimation });
			$.ajax({
			  url: 'searchSubCatByCateId.html',
			  data : {'cateId' : $(this).val()},
			  dataType : 'json',
			  success: function(data) {
				  	var catHtml = "";
					$.each(data.subCatList, function() {
						catHtml += '<option value="' + this.value + '">' + this.name + '</option>';
					});	
					$('select[name="subCateId"]').html($('select[name="subCateId"] :first').clone().wrap('<div>').parent().html() + catHtml);										
			  }
			});
			$.ajax({
			  url: 'get-returned-date.html',
			  data : {'cateId' : $(this).val(), 'fromdate' : $('#fromDate').val()},
			  dataType : 'json',
			  success: function(data) {
				  $('#toDate').val(data.returnedDate);
			  }
			});
		}
	});
	
	var currentTime = new Date();
	var month = currentTime.getMonth() + 1;
	var day = currentTime.getDate();
	if(month < 10) month = "0" + month;
	if(day < 10) day = "0" + day;
	var year = currentTime.getFullYear();
	
	var today = day + "/" + month + "/" + year;
	
	if($("#fromDate").val()=="")
  		$("#fromDate").val(today);
});
</script>